package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.TypeAttributeMapper;
import com.yunban.project.template.model.domain.TypeAttribute;
import com.yunban.project.template.model.dto.BatchSaveTypeAttributeReq;
import com.yunban.project.template.model.dto.TypeAttributeReq;
import com.yunban.project.template.model.vo.TypeAttributeResp;
import com.yunban.project.template.service.TypeAttributeService;
import com.yunban.project.template.service.TypeService;
import jakarta.annotation.Resource;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lijiajun
 * @date: 2024-11-07
 * @version: 1.0
 */
@Service
public class TypeAttributeServiceImpl extends ServiceImpl<TypeAttributeMapper, TypeAttribute> implements TypeAttributeService {

    @Resource
    private TypeService typeService;

    @Override
    public TypeAttribute findById(long id) {
        TypeAttribute typeAttribute = this.getById(id);
        if (ObjectUtils.isEmpty(typeAttribute)) {
            throw new BusinessException("此类型不存在");
        }
        return typeAttribute;
    }

    @Override
    public List<TypeAttributeResp> getAllTypeAttribute() {
        List<TypeAttribute> list = this.list();
        return SmartPageUtil.convert(list, TypeAttributeResp.class);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Boolean batchSave(BatchSaveTypeAttributeReq req) {
        // 检查类型是否存在
        this.typeService.findById(req.getBlockTypeId());
        // 批量入库
        List<TypeAttribute> typeAttributes = new ArrayList<>(req.getAttributeList().size());
        req.getAttributeList().forEach(items -> {
            TypeAttribute typeAttribute = new TypeAttribute();
            typeAttribute.setBlockTypeId(req.getBlockTypeId());
            typeAttribute.setName(items.getName());
            typeAttribute.setCode(items.getCode());
            typeAttributes.add(typeAttribute);
        });
        this.saveBatch(typeAttributes);
        return true;
    }

    @Override
    public Boolean saveTypeAttribute(TypeAttributeReq req) {
        // 检查类型是否存在
        this.typeService.findById(req.getBlockTypeId());
        TypeAttribute typeAttribute = new TypeAttribute();
        BeanUtils.copyProperties(req, typeAttribute);
        return this.save(typeAttribute);
    }

    @Override
    public Boolean editTypeAttribute(long id, TypeAttributeReq req) {
        TypeAttribute typeAttribute = this.findById(id);
        BeanUtils.copyProperties(req, typeAttribute);
        return this.updateById(typeAttribute);
    }

    @Override
    public Boolean delTypeAttribute(long id) {
        TypeAttribute typeAttribute = this.findById(id);
        return this.removeById(typeAttribute);
    }
}
